package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.LoginBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.LoginModel;

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    EditText userphone;
    EditText password;
    Button btnlogin;
    TextView login_regist;
    CheckBox login_jzmm;
    ImageView login_seepassword;
    String userid;
    TextView login_wjmm,login_qtfsdl;

    private Dialog mDialog;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_login);
        userphone = (EditText)findViewById(R.id.userphone);
        password = (EditText)findViewById(R.id.password);
        btnlogin = (Button)findViewById(R.id.btnlogin);
        login_regist = (TextView) findViewById(R.id.login_regist);
        login_jzmm = (CheckBox)findViewById(R.id.login_jzmm);
        login_seepassword =(ImageView)findViewById(R.id.login_seepassword);
        login_wjmm = (TextView)findViewById(R.id.login_wjmm);
        login_qtfsdl = (TextView)findViewById(R.id.login_qtfsdl);
        boolean jzmm = getUer_remember();

        if (jzmm){
            String u = getuser();
            String p = getpass();
            userphone.setText(u);
            password.setText(p);
            login_jzmm.setChecked(jzmm);
        }
    }

    @Override
    void initEvent() {
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userphones = userphone.getText().toString().trim();
                String passwords = password.getText().toString().trim();
                if (userphones.equals("")||passwords.equals("")){
                    Toast.makeText(LoginActivity.this,"账号或密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else {
                    mDialog = WeiboDialogUtils.createLoadingDialog(LoginActivity.this, "登录中...");
                    LoginModel loginModel = new LoginModel();
                    loginModel.login(userphones,passwords,loginBeanTListener);
                }
            }
        });
        login_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivityForResult(intent,1);
            }
        });
        login_seepassword.setOnClickListener(this);

        login_wjmm.setOnClickListener(this);
        login_qtfsdl.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    TListener<LoginBean> loginBeanTListener = new TListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            String success = loginBean.getSuccess();
            String flag = loginBean.getFlag();
             userid = loginBean.getUserid();

            if (success.equals("1")){
                if (flag.equals("1")){
                    saveUserInfo();
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    DialogThridUtils.closeDialog(mDialog);
                }
                else if (flag.equals("2")){
                    Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                    DialogThridUtils.closeDialog(mDialog);
                }
                else if (flag.equals("3")){
                    Toast.makeText(LoginActivity.this,"账号错误",Toast.LENGTH_SHORT).show();
                    DialogThridUtils.closeDialog(mDialog);
                }
                else if(flag.equals("4")){
                    Toast.makeText(LoginActivity.this,"账号已被冻结",Toast.LENGTH_SHORT).show();
                    DialogThridUtils.closeDialog(mDialog);
                }
                else if (flag.equals("5")){
                    saveUserInfo();
                    Toast.makeText(LoginActivity.this,"当前账号禁止评论",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    DialogThridUtils.closeDialog(mDialog);
                }

            }
            else if(success.equals("0")){
                Toast.makeText(LoginActivity.this,"登陆错误",Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }

            else{
                Toast.makeText(LoginActivity.this,"111",Toast.LENGTH_SHORT).show();
                DialogThridUtils.closeDialog(mDialog);
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this,"222",Toast.LENGTH_SHORT).show();
            DialogThridUtils.closeDialog(mDialog);
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if(resultCode == RESULT_OK){
                    String phone = data.getStringExtra("phone");
                    userphone.setText(phone);
                    break;
                }
            case 2:

        }

    }

    private void saveUserInfo(){//存储用户信息
        SharedPreferences.Editor editor = getSharedPreferences("userInfo",MODE_PRIVATE).edit();
        editor.putString("userid",userid);
        editor.putBoolean("remember",login_jzmm.isChecked());
        if(login_jzmm.isChecked()){
            editor.putString("userphone",userphone.getText().toString());
            editor.putString("password",password.getText().toString());
        }
        editor.commit();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_seepassword:
                setPasswordVisibility();break;
            case R.id.login_wjmm:
                Intent intentp = new Intent(LoginActivity.this,RetrievePasswordActivity.class);
                startActivity(intentp);
                break;
            case R.id.login_qtfsdl:
                Toast.makeText(LoginActivity.this,"等待开发",Toast.LENGTH_SHORT).show();
                break;


        }
    }

    private void setPasswordVisibility(){
        if (login_seepassword.isSelected()) {
            login_seepassword.setSelected(false);
            password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            login_seepassword.setSelected(true);
            password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }
}
